﻿/*************************   Auto Generated Repository Code For Patient table     Generated @ 4/23/2013 7:28:29 PM *************************/


using System;
using System.Collections.Generic;
using System.Linq;
using BusinessObjects;

namespace DataLayer
{
    public partial class PatientRepository
    {
        private PracticeSimpleDataContext dB;


        /// <summary>
        /// Retrieve an Patient by its PatientID (Primary Key)
        /// </summary>
        /// <param name="PatientID">The ID of the <see cref="Patient"/> to retrieve</param>
        /// <returns>A <see cref="Patient"/> object</returns>
        public BusinessObjects.Patient Select(int PatientID)
        {
            Patient retObject;

            using (dB = new PracticeSimpleDataContext())
            {
                retObject = (from x in dB.Patients where x.PatientID == PatientID select x).SingleOrDefault();
            }

            return ConvertToBusinessObject(retObject);
        }

        /// <summary>
        /// Return a list of Patient objects
        /// </summary>
        /// <returns>A List of type <see cref="Patient"/></returns>
        public List<BusinessObjects.Patient> SelectAll()
        {
            List<BusinessObjects.Patient> retObject = new List<BusinessObjects.Patient>();

            using (dB = new PracticeSimpleDataContext())
            {
                var PatientList = (from x in dB.Patients select x).ToList();

                foreach (Patient obj in PatientList)
                {
                    retObject.Add(ConvertToBusinessObject(obj));
                }

            }
            return retObject;
        }

        /// <summary>
        /// Accepts a Patient Business object, converts it and saves it to the database
        /// </summary>
        /// <returns>Void</returns>
        public int Save(BusinessObjects.Patient businessObject)
        {
            Patient dbPatient;
            using (dB = new PracticeSimpleDataContext())
            {
                 dbPatient = ConvertToDataObject(businessObject);
                dB.Patients.InsertOnSubmit(dbPatient);
                dB.SubmitChanges();
            }

            return dbPatient.PatientID;
        }

        /// <summary>
        /// Accepts the Primary Key of a Patient, finds it in the database and deletes it
        /// </summary>
        /// <param name="PatientID">The ID of the <see cref="Patient"/> to retrieve</param>
        /// <returns>Void</returns>
        public void Delete(int PatientID)
        {
            using (dB = new PracticeSimpleDataContext())
            {
                DataLayer.Patient obj = (from x in dB.Patients where x.PatientID == PatientID select x).SingleOrDefault();
                dB.Patients.DeleteOnSubmit(obj);
                dB.SubmitChanges();
            }
        }

        /// <summary>
        /// Converts a Patient data object to a Patient business object
        /// </summary>
        /// <returns>A business object of type <see cref="Patient"/></returns>
        public BusinessObjects.Patient ConvertToBusinessObject(DataLayer.Patient dataObject)
        {
            BusinessObjects.Patient businessObject = new BusinessObjects.Patient();

            businessObject.PatientID = dataObject.PatientID;
            businessObject.FName = dataObject.FName;
            businessObject.LName = dataObject.LName;
            businessObject.DateOfBirth = dataObject.DateOfBirth;
            businessObject.FacilityID = dataObject.FacilityID;
            businessObject.SSN = dataObject.SSN;


            return businessObject;

        }

        /// <summary>
        /// Converts a Patient business object to a Patient data object
        /// </summary>
        /// <returns>A data object of type <see cref="Patient"/></returns>
        public DataLayer.Patient ConvertToDataObject(BusinessObjects.Patient businessObject)
        {
            DataLayer.Patient dataObject = new DataLayer.Patient();

            dataObject.PatientID = businessObject.PatientID;
            dataObject.FName = businessObject.FName;
            dataObject.LName = businessObject.LName;
            dataObject.DateOfBirth = businessObject.DateOfBirth;
            dataObject.FacilityID = businessObject.FacilityID;
            dataObject.SSN = businessObject.SSN;


            return dataObject;
        }
    }
}


